function out = estimate_fun(data1,conv_ma,I,ng,param_ini)

% modified for RAND round 1 revision on July 19, 2022

objfun0 = @(x) -ll_fun5([x(1); x(2); x(3); x(4); conv_ma*x(5:ng+4)';...
    conv_ma*x(5+ng:2*ng+4)';...
    x(2*ng+5:2*ng+6)'; x(2*ng+7)],data1,I);

%% do the search 4 times.. 
options = optimset('Display', 'iter', 'MaxFunEvals',1000,'MaxIter',1000,...
    'TolFun',.000000001,'TolX',.0000001);
x1 = fminsearch(objfun0,param_ini,options);  % vector of estimated parameters

% second use the estimates from first trial as starting value for the next
options = optimset('Display', 'iter', 'MaxFunEvals',600,'MaxIter',600,...
    'TolFun',.00001,'TolX',.00001);
x2 = fminsearch(objfun0,x1,options);
% third  use the estimates from second as starting guess
options = optimset('Display', 'iter', 'MaxFunEvals',1500,'MaxIter',1500,...
    'TolFun',.00001,'TolX',.00001);
x3 = fminsearch(objfun0,x2,options);
% fourth and final 
options = optimset('Display', 'iter', 'MaxFunEvals',1500,'MaxIter',1500,...
    'TolFun',.00001,'TolX',.00001);
x = fminsearch(objfun0,x3,options);

[objvalue, lowuest] = ll_fun5([x(1); x(2); x(3); x(4); conv_ma*x(5:ng+4)'; conv_ma*x(5+ng:2*ng+4)';...
     x(2*ng+5:2*ng+6)'; x(2*ng+7)],data1,I);
 
 out = [x lowuest objvalue];

end


